var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function(req, res, next) {
    // if(req.query.test_login== "1"){
    //     if(req.session.username == undefined || req.session.username == "")
    //         res.send('0');
    // }
    // else{
        var db=new sqlite3.Database(path.join(__dirname, "../db/LH.s3db"));
        try{
            sqlStr1="select * from college where college_name == ?";
            sqlStr2="select * from commitee where com_name == ?";
            sqlStr3="select * from manager where m_id like ? order by m_id desc limit 0,1";
            // db.all(sqlStr4, [req.session.username+"%"],function(err, rows)
            db.all(sqlStr1, [req.query.mcollege],function(err, rows){
                //req.session.username;
                if(!rows[0])
                {
                    cono="-1";//req.session.username
                }
                else
                {
                    cono=rows[0]["college_no"];
                }
                db.all(sqlStr3, [cono+"%"],function(err, rows)
                {
                    var newno='';
                    if(!rows[0])
                    {
                        newno=cono+"01";
                    }
                    else
                    {
                        newno=parseInt(rows[0]["m_id"])+1;
                    }
                            var mid =newno;
                            var mname=req.query.mname;
                            var mcollegeno = cono;
                            var cid=req.session.username;
                            var mpwd = "123";
                            
                            db.run("INSERT INTO manager VALUES(?,?,?,?,?)",
                            mid,mname,mcollegeno,cid,mpwd,function(err){
                                if(err==null){
                                    res.send("1");
                                }else{
                                    res.send("0");
                                }
                            });
                });

            });
            
           
        }catch(ex){
            console.log(ex);
        };
});
module.exports = router;
